# Definition for a binary tree node.
class TreeNode:
    def __init__(self, val=0, left=None, right=None):
        self.val = val
        self.left = left
        self.right = right


from typing import *


class Solution:
    def rangeSumBST(self, root: Optional[TreeNode], low: int, high: int) -> int:

        tot = 0

        def f(root: TreeNode):
            if root is None:
                return
            v = root.val
            if v >= low and v <= high:
                nonlocal tot
                tot += v
            f(root.left)
            f(root.right)

        f(root)
        return tot
